package com.lz.wsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lz.wsy.mapper.BookMapper;
import com.lz.wsy.pojo.Book;
import com.lz.wsy.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 电子样册Service实现
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
        implements BookService {

    @Override
    public Map<String, Object> pageVo(Integer pageNumber, Integer pageSize, Book book) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        //翻页查询 page(page–翻页对象,queryWrapper实体对象封装操作类)
        wrapper.like(StringUtils.hasLength(book.getBookName()),
                Book::getBookName,book.getBookName());
        IPage<Book> page = this.page(new Page<Book>(pageNumber, pageSize), wrapper);
        Map<String, Object> map = new HashMap<>();
        //获取分页列表数据
        map.put("list", page.getRecords());
        //获取总记录数
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Book get(Integer bookId) {
        return baseMapper.selectById(bookId);
    }

    @Override
    public boolean addAndEdit(Book book) {
        //获取登陆用户信息
        /*LoginUser loginuser = SecurityUtil.getLoginuser();
        book.setCreateUser(loginuser.getUsername());
        book.setUpdateUser(loginuser.getUsername());*/
        book.setUpdateTime(new Date());
        if(null == book.getBookId()){
            book.setCreateTime(new Date());
            baseMapper.insert(book);
        }else {
            baseMapper.updateById(book);
        }

        return true;
    }

    @Override
    public boolean delBook(Integer bookId) {
        return SqlHelper.retBool(baseMapper.deleteById(bookId));
    }
}




